package x.amadeus.announcement.controller;

import java.util.List;

import javax.annotation.Resource;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.itcast.commons.CommonUtils;

import x.amadeus.announcement.domain.Announcement;
import x.amadeus.announcement.domain.Message;
import x.amadeus.announcement.service.AnnouncementService;
import x.amadeus.utils.Result;

@Controller
@RequestMapping("/announcement")
public class AnnouncementController {

	@Resource(name="announcementService")
	private AnnouncementService announcementService;
	
	
	@RequestMapping("/delete")
	@ResponseBody
	public Result delete(HttpServletRequest request, HttpServletResponse response)
	{
		String aid = request.getParameter("aid");
		announcementService.delete(aid);
		
		Result result = new Result();
		
		result.setCode(1);
		result.setMessage("删除成功");
		result.setData(aid);
		
		return result;
	}
	
	
	@RequestMapping("/edit")
	@ResponseBody
	public Result edit(HttpServletRequest request, HttpServletResponse response)
	{
		Announcement announcement = CommonUtils.toBean(request.getParameterMap(), Announcement.class);
		announcementService.edit(announcement);
		
		Result result = new Result();
		
		result.setCode(1);
		result.setMessage("修改成功");
		result.setData(announcement);
		
		return result;
	}
	
	@RequestMapping("/add")
	@ResponseBody
	public Result add(HttpServletRequest request, HttpServletResponse response)
	{
		Announcement announcement = CommonUtils.toBean(request.getParameterMap(), Announcement.class);
		announcement.setAid(CommonUtils.uuid());
		
		announcementService.add(announcement);
		
		Result result = new Result();
		
		result.setCode(1);
		result.setMessage("添加成功");
		result.setData(announcement.getAid());
		
		return result;
	}
	
	
	
	@RequestMapping("/findByAid")
	@ResponseBody
	public Result findByAid(HttpServletRequest request, HttpServletResponse response)
	{
		String aid = request.getParameter("aid");
		
		Announcement announcement = announcementService.findByAid(aid);
		
		Result result = new Result();
		
		result.setCode(1);
		result.setMessage("查询成功");
		result.setData(announcement);
		
		return result;
	}
	
	
	@RequestMapping("findAll")
	@ResponseBody
	public Result findAll(HttpServletRequest request, HttpServletResponse response)
	{
		List<Announcement> announcementList = announcementService.findAll();
		
		Result result = new Result();
		
		result.setCode(1);
		result.setMessage("查找成功");
		result.setData(announcementList);
		
		return result;
	}
	
	@RequestMapping("/addM")
	@ResponseBody
	public Result addM(HttpServletRequest request, HttpServletResponse response)
	{
		Message msg = CommonUtils.toBean(request.getParameterMap(), Message.class);
		System.out.println(msg.getMmid());
		if(msg.getMmid()==null)
		{
			msg.setMmid(CommonUtils.uuid());
		}
		
		announcementService.addM(msg);
		
		Result result = new Result();
		result.setData(msg);
		result.setCode(1);
		result.setMessage("添加成功");
		
		return result;
		
	}
	
	@RequestMapping("/findAllM")
	@ResponseBody
	public Result findAllM(HttpServletRequest request, HttpServletResponse response)
	{
		List<Message> msgList = announcementService.findAllM();
		
		Result result = new Result();
		
		result.setCode(1);
		result.setMessage("查找成功");
		result.setData(msgList);
		
		return result;
	}
	
}
